﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using Taobao.EF;
using Taobao.Filter;
using Taobao.Manger;
using Taobao.Models;
using Taobao.Public;

namespace Taobao.Controllers
{

    [CheckLogin(true)]
    public class HomeController : BaseController
    {
        // TuiGuangEntities1 db = new TuiGuangEntities1();

        [CheckLogin(false)] 
        public ActionResult Index()
        {

            return View();
        }
        /// <summary>
        /// 搜索店铺
        /// </summary>
        /// <param name="shopName"></param>
        /// <returns></returns>
        [CheckLogin(true)]
        public JsonResult Search(string shopName)
        {
            JsonResult js = new JsonResult();

            if (string.IsNullOrEmpty(shopName))
            {
                return null;
            }
            HomeManger hm = new HomeManger();
            var ret = hm.GetHtmlByKey(shopName);
            if (ret == null)
            {
                js = CustomClass.GetJsonRet(NoticeMsg.没有数据);
            }
            else
            {
                js = CustomClass.GetJsonRet(NoticeMsg.成功, ret);
            }

            return js;
        }
        /// <summary>
        /// 接收店铺url获取商品数据
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        [HttpGet]
        [CheckLogin(false)]
        public ActionResult Detail(string url)
        {
            HomeManger hm = new HomeManger();
            List<auctionsInshop> item = new List<auctionsInshop>();
            //先读缓存
            var shop = HttpContext.Cache.Get(Constant.lastShopUrl);
            if (shop != null && !string.IsNullOrEmpty(shop.ToString()) && shop.ToString().Trim() == "https:" + url.Trim())
            {
                item = (List<auctionsInshop>)HttpContext.Cache.Get(Constant.productdetail);

            }
            else
            {
                item = hm.GetDetailList(url);
            }

            ViewBag.data = item;
            if (ViewBag.data != null)
            {
                return View(ViewBag.data);
            }
            else
            {
                return View();
            }
        }

        [HttpPost]
        public JsonResult ProductSearch(string productName)
        {
            if (string.IsNullOrEmpty(productName))
            {
                return null;
            }

            var keyWord = HttpUtility.UrlEncode(productName, Encoding.GetEncoding("gbk"));
            string sear = "/search.htm?q=";
            var item = HttpContext.Cache.Get(Constant.productdetail);
            string shopurl = "";//店铺内搜索url
            if (item != null)
            {
                shopurl = ((List<auctionsInshop>)item)[0].searchUrl;
            }
            HomeManger hm = new HomeManger();
            shopurl = shopurl + sear + keyWord;
            var detail = hm.GetDetailList(shopurl);

            JsonResult jr = new JsonResult();


            return jr;
        }
        /// <summary>
        /// 保存到数据库
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns> 
        public JsonResult Save(string url)
        {
            JsonResult jr = new JsonResult();
            if (string.IsNullOrEmpty(url))
            {
                jr = CustomClass.GetJsonRet(NoticeMsg.没有数据);
            }
            else
            {
                var list = url.Split(',').ToList();
                List<auctionsInshop> proList = new List<auctionsInshop>();
                var prodetail = HttpContext.Cache.Get(Constant.productdetail);
                if (prodetail != null)
                {
                    proList = (List<auctionsInshop>)prodetail;
                    HomeManger hm = new HomeManger();
                    var userId = ViewBag.userId;

                    var ret = hm.SaveProduct(list, proList, Convert.ToInt32(userId));
                    if (ret)
                    {
                        jr = CustomClass.GetJsonRet(NoticeMsg.成功);
                    }
                    else
                    {
                        jr = CustomClass.GetJsonRet(NoticeMsg.失败);
                    }
                }
                else
                {
                    jr = CustomClass.GetJsonRet(NoticeMsg.请重新刷新页面);
                }
            }
            return jr;

        }


    }
}